package com.sanalkitaplik.admin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.apache.geronimo.crypto.asn1.x509.UserNotice;

import com.sanalkitaplik.hibernateutil.AdminsUtil;
import com.sanalkitaplik.hibernateutil.AuthorizationsUtil;
import com.sanalkitaplik.packageclassdto.AdminInfoDTO;
import com.sanalkitaplik.packageclassdto.AdminLoginInfoDTO;
import com.sun.org.apache.regexp.internal.recompile;

public class AdminAddNewAdminManager {
	
	private String adminUserName;
	private String password;
	private String passwordCheck;
	private String email;
	private String firstName;
	private String lastName;
	private String group;
	private List<SelectItem> groups;
	
	private boolean errorUsername;
	private boolean errorPassword;
	private boolean errorEmail;
	private boolean errorFirstName;
	private boolean errorLastName;
	private boolean errorUserNameExists;
	
	private boolean success;
	
	public AdminAddNewAdminManager() {
		success = false;
		resetErrors();
		resetForm();
		this.groups = new ArrayList<SelectItem>();
		List<String> groupNames = AuthorizationsUtil.getGroupNames();
		for (int i=0; i<groupNames.size(); i++)
			this.groups.add(new SelectItem(groupNames.get(i)));
	}
	
	private void resetErrors() {
		this.errorUsername = false;
		this.errorPassword = false;
		this.errorFirstName = false;
		this.errorLastName = false;
		this.errorEmail = false;
		this.errorUserNameExists = false;
	}
	
	public List<SelectItem> getGroups() {
		return groups;
	}

	public void setAdminUserName(String adminUserName) {
		this.adminUserName = adminUserName;
	}
	
	public String getAdminUserName() {
		return adminUserName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setPasswordCheck(String passwordCheck) {
		this.passwordCheck = passwordCheck;
	}

	public String getPasswordCheck() {
		return passwordCheck;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getGroup() {
		return group;
	}
	
	public boolean getErrorUsername() {
		return errorUsername;
	}

	public boolean getErrorPassword() {
		return errorPassword;
	}

	public boolean getErrorEmail() {
		return errorEmail;
	}

	public boolean getErrorFirstName() {
		return errorFirstName;
	}

	public boolean getErrorLastName() {
		return errorLastName;
	}

	public boolean getErrorUserNameExists() {
		return errorUserNameExists;
	}
	
	public boolean getSuccess() {
		return success;
	}
	
	public void addNewAdmin(ActionEvent event) {
		int a;
		resetErrors();
		if(adminUserName.length()<5 || adminUserName.length()>20){
			errorUsername = true;
		}
		if(password.length()<5 || password.length()>20 || !password.equals(passwordCheck)){
			errorPassword = true;
		}
		if (email.length()<5 || email.length()>20 ||
				(a = email.indexOf('@')) <= 0 || a >= email.length()-1) {
			errorEmail = true;
		}
		if(firstName.length()<5 || firstName.length()>20){
			errorFirstName = true;
		}
		if(lastName.length()<5 || lastName.length()>20){
			errorLastName = true;
		}
		if (!(errorUsername || errorPassword || errorEmail || errorFirstName || errorLastName)) {	

			AdminLoginInfoDTO loginInfo = new AdminLoginInfoDTO() ;
			AdminInfoDTO info = new AdminInfoDTO();
			
			info.setFirst_name(firstName);
			info.setLast_name(lastName);
			loginInfo.setAdmin_username(adminUserName);
			loginInfo.setAdmin_password(password);
			loginInfo.setAdmin_email(email);
			
			try {
				if (AdminsUtil.adminRegister(info, loginInfo, group))
					success = true;
				else
					errorUserNameExists = true;
			} catch (SQLException e) {
				e.printStackTrace();
			}
	
		}
	}
	
	private void resetForm() {
		this.adminUserName = "";
		this.password = "";
		this.passwordCheck = "";
		this.email = "";
		this.firstName = "";
		this.lastName = "";
	}
	
	public void addAnotherAdmin(ActionEvent event) {
		resetForm();
		resetErrors();
		success = false;
	}

}
	
  